﻿package mapEditor.view
{
	import asset.AssetBase;
	import asset.mapEditor.renAsset;
	import asset.mapEditor.toolZoomInAsset;
	import asset.mapEditor.toolZoomOutAsset;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.greensock.plugins.TransformAroundPointPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Timer;
	import flash.utils.describeType;
	
	import iso.IsoGrid;
	import iso.IsoPoint;
	import iso.IsoScene;
	import iso.IsoSprite;
	import iso.IsoUtils;
	
	import mapEditor.common.MapAssetManager;
	import mapEditor.common.RoadGrid;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.EditorModel;
	import mapEditor.vo.MapAssetVO;
	import mapEditor.vo.MapGridVO;
	import mapEditor.vo.ToolType;
	
	import road.AStar;
	
	public class MapSceneView extends Sprite
	{
		private var _model:EditorModel;
		private var _mapAStar:AStar;
		private var _testBox:AssetBase;
		private var _mapViewBase:IsoScene;//地图资源物件层
		private var _mapGridLayer:IsoGrid;//地图网格层
		private var _roadGridLayer:RoadGrid;//地图路点层
		private var _mapTopLayer:IsoSprite;//地图最顶层（可存放临时对象）
		
		private var _mapPathArray:Array;//移动路径集
		private var _mapMoveNode:Array;//当前移动路径节点
		private var _targetPos:IsoPoint;//移动至的目标点
		private var _mapSpeed:Number=5;//移动速度倍率
		private var _mouseAssetDis:DisplayObject;//鼠标跟随物件对象
		private var _mouseAsset:MapAssetBase;//鼠标跟随物件Iso对象
		private var _testMapAssetVO:MapAssetVO;
		private var _mouseIsTool:Boolean;//鼠标跟随物件是否为工具图标
		private var _selectAsset:MapAssetBase;//在选择工具下，当前被选中的物件
		private var _selectAssetAcBox:Shape;//选中的物件的边框
		private var _actionTimer:Timer;//用与寻路行动
		
		public function MapSceneView(model:EditorModel)
		{
			_model=model;
			setView();
			setEvent();
		}
		
		private function setView():void
		{
			_mapViewBase=new IsoScene();
			addChild(_mapViewBase);

			_mapGridLayer=new IsoGrid(_model.mapVO.mapType, _model.mapVO.cellSize, _model.mapVO.mapRow, _model.mapVO.mapCols);
			addChild(_mapGridLayer);
			
			_roadGridLayer=new RoadGrid(_model);
			_roadGridLayer.mouseEnabled=false;
			addChild(_roadGridLayer);
			
			_mapTopLayer=new IsoSprite();
			addChild(_mapTopLayer);
			
			_selectAssetAcBox=new Shape();
			
			TweenPlugin.activate([TransformAroundPointPlugin]);
			
			_actionTimer=new Timer(1000/25);
			_actionTimer.addEventListener(TimerEvent.TIMER, onTestBoxMove);
		}
		
		private function setEvent():void
		{
			_model.addEventListener(MapEditorEvent.TOOL_TYPE_CHANGE, __onToolTypeChange);
			_model.addEventListener(MapEditorEvent.MOUSE_ASSET_CHANGE, __onMouseAssetChange);
			_model.addEventListener(MapEditorEvent.MAP_ASSET_MIRROR, __onMapAssetMirror);
			_model.addEventListener(MapEditorEvent.MAP_ASSET_BOTTOM, __onMapAssetBottom);
			_model.addEventListener(MapEditorEvent.MAP_ASSET_FLOOR, __onMapAssetFloor);
			_model.addEventListener(MapEditorEvent.MAP_ASSET_GRID, __onMapAssetGrid);
			
			_mapGridLayer.addEventListener(MouseEvent.CLICK, __onClick);
			_mapGridLayer.addEventListener(MouseEvent.MOUSE_DOWN, __onMouseDown);
			_mapGridLayer.addEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			_mapGridLayer.addEventListener(MouseEvent.MOUSE_OUT, __onMouseOut);
			_mapGridLayer.addEventListener(MouseEvent.MOUSE_MOVE, __onMouseMove)
		}
		
		/**
		 *镜像物件
		 */		
		private function __onMapAssetMirror(evt:MapEditorEvent):void
		{
			switch(_model.toolType)
			{
				case ToolType.MAP_ADD_ASSET:
					if(_mouseAsset) _mouseAsset.scaleX=Boolean(evt.date) ? -1 : 1;
					break;
				case ToolType.TOOL_SELECT:
					if(_selectAsset)
					{
						_selectAsset.mapAssetVO.isMirror=Boolean(evt.date);
						_selectAsset.scaleX=Boolean(evt.date) ? -1 : 1;
					}
					break;
			}
		}
		
		/**
		 *置底选中的物件
		 */		
		private function __onMapAssetBottom(evt:MapEditorEvent):void
		{
			switch(_model.toolType)
			{
				case ToolType.TOOL_SELECT:
					if(_selectAsset)
					{
						_selectAsset.parent.setChildIndex(_selectAsset, 0);
						_selectAsset.mapAssetVO.isBottom=Boolean(evt.date);
					}
					break;
			}
		}
		
		/**
		 *置于地表选中的物件 
		 */		
		private function __onMapAssetFloor(evt:MapEditorEvent):void
		{
			switch(_model.toolType)
			{
				case ToolType.TOOL_SELECT:
					if(_selectAsset)
					{
						if(Boolean(evt.date))
						{
							_mapViewBase.moveToFloor(_selectAsset);
						}
						else
						{
							_mapViewBase.moveToAsset(_selectAsset);
						}
						_selectAsset.mapAssetVO.isFloor=Boolean(evt.date);
					}
					break;
			}
		}
		
		/**
		 *贴近网格选中的物件(在同一个地图层，一个网格只允许一个同样的物件)
		 */		
		private function __onMapAssetGrid(evt:MapEditorEvent):void
		{
			switch(_model.toolType)
			{
				case ToolType.TOOL_SELECT:
					if(_selectAsset)
					{
						_selectAsset.mapAssetVO.isGrid=Boolean(evt.date);
						if(Boolean(evt.date))
						{
							var pt:IsoPoint=_selectAsset.position;
							pt.x = Math.round(pt.x / _model.mapVO.cellSize) * _model.mapVO.cellSize;
							pt.y = Math.round(pt.y / _model.mapVO.cellSize) * _model.mapVO.cellSize;
							pt.z = Math.round(pt.z / _model.mapVO.cellSize) * _model.mapVO.cellSize;
							_selectAsset.position = pt;
						}
					}
					break;
			}
		}
		
		/**
		 *当操作工具类型改变时 
		 */		
		private function __onToolTypeChange(evt:MapEditorEvent):void
		{
			if(_selectAssetAcBox.parent)_selectAssetAcBox.parent.removeChild(_selectAssetAcBox);
			switch(evt.date.toString())
			{
				case ToolType.TEST_MAP://测试地图操作类型
					testMap();
					break;
				case ToolType.TOOL_SELECT://选择工具
					_mouseIsTool=false;
					break;
				case ToolType.TOOL_ZOOM_IN://放大工具
				case ToolType.TOOL_ZOOM_OUT://缩小工具
					_mouseIsTool=true;
					addMouseAsset();
					break;
			}
		}
		
		/**
		 *当前鼠标跟随资源改变 
		 */		
		private function __onMouseAssetChange(evt:MapEditorEvent):void
		{
			addMouseAsset();
		}
		
		/**
		 *路点层上单击左键处理 
		 */		
		private function __onClick(evt:MouseEvent):void
		{
			var p:Point = new Point(_mapGridLayer.mouseX, _mapGridLayer.mouseY);
			switch(_model.toolType)
			{
				case ToolType.ROAD_BLOCK://设置路点障碍
					_roadGridLayer.addRoadPoint(p, 1);
					stopDrag();//停止移动地图(自已)
					break;
				case ToolType.CLEAR_ROAD_BLOCK://清除路点障碍
					_roadGridLayer.clearRoadPoint(p);
					stopDrag();//停止移动地图(自已)
					break;
				case ToolType.TEST_MAP://测试地图
					boxMove(p);
					break;
				case ToolType.MAP_ADD_ASSET://地图场景放置物件
					addMapAsset(p);
					break;
				case ToolType.TOOL_ZOOM_IN://放大工具
					TweenLite.to(this, 1, {transformAroundPoint:{point:new Point(evt.localX,evt.localY), scaleX:this.scaleX+0.5, scaleY:this.scaleX+0.5}, ease:Back.easeOut});
					break;
				case ToolType.TOOL_ZOOM_OUT://缩小工具
					TweenLite.to(this, 1, {transformAroundPoint:{point:new Point(evt.localX,evt.localY), scaleX:this.scaleX-0.5, scaleY:this.scaleX-0.5}, ease:Back.easeOut});
					break;
			}
		}
		
		private function __onMouseDown(evt:MouseEvent):void
		{
			moveMapScene();
			_mapGridLayer.addEventListener(MouseEvent.MOUSE_UP, __onMouseUp);
			
			switch(_model.toolType)
			{
				case ToolType.TOOL_SELECT://选择工具
					assetSelect(evt);
					break;
			}
		}
		
		private function __onMouseUp(evt:MouseEvent):void
		{
			_mapGridLayer.removeEventListener(MouseEvent.MOUSE_UP, __onMouseUp);
			stopDrag();//停止移动地图(自已)
			
			switch(_model.toolType)
			{
				case ToolType.TOOL_SELECT:
					if(_selectAsset) _selectAsset.stopDrag();
					break;
			}
			
		}
		
		private function __onMouseMove(evt:MouseEvent):void
		{
			var p:Point = new Point(_mapGridLayer.mouseX, _mapGridLayer.mouseY);
			switch(_model.toolType)
			{
				case ToolType.ROAD_BLOCK://设置路点障碍
					if(evt.buttonDown) _roadGridLayer.addRoadPoint(p, 1);
					break;
				case ToolType.CLEAR_ROAD_BLOCK://清除路点障碍
					if(evt.buttonDown) _roadGridLayer.clearRoadPoint(p);
					break;
				case ToolType.MAP_ADD_ASSET://地图场景放置物件
					if(evt.buttonDown && _model.assetIsGrid) addMapAsset(p);
					if(_mouseAsset)
					{//物件跟随鼠标移动
						if(_model.assetIsGrid)
						{//如果当前物件需要贴近格子摆放
							var pt:IsoPoint=IsoUtils.screenToSpace(p);
							//							pt.x=pt.x+_mouseAsset.height;
							//							pt.z=pt.z+_mouseAsset.height;
							pt.x=Math.floor(pt.x/_model.mapVO.cellSize)*_model.mapVO.cellSize;
							pt.y=Math.floor(pt.y/_model.mapVO.cellSize)*_model.mapVO.cellSize;
							pt.z=Math.floor(pt.z/_model.mapVO.cellSize)*_model.mapVO.cellSize;
							
							_mouseAsset.position=pt;
						}
						else
						{
							//							p.y = p.y+_mouseAsset.height;
							//							p.x = p.x+_mouseAsset.width/2;
							_mouseAsset.position=IsoUtils.screenToSpace(p);
						}
						_mapViewBase.depthSort();
					}
					break;
				case ToolType.TOOL_ZOOM_IN://放大工具
				case ToolType.TOOL_ZOOM_OUT://缩小工具
					if(_mouseAsset)
					{//物件跟随鼠标移动
						_mouseAsset.setScreenPoint(new Point(stage.mouseX, stage.mouseY));
					}
					break;
				case ToolType.TOOL_SELECT:
					if(_selectAsset && evt.buttonDown)
					{
						if(_model.assetIsGrid)
						{//如果当前物件需要贴近格子摆放
							pt=IsoUtils.screenToSpace(p);
							//							pt.x=pt.x+_selectAsset.height;
							//							pt.z=pt.z+_selectAsset.height;
							pt.x=Math.floor(pt.x/_model.mapVO.cellSize)*_model.mapVO.cellSize;
							pt.y=Math.floor(pt.y/_model.mapVO.cellSize)*_model.mapVO.cellSize;
							pt.z=Math.floor(pt.z/_model.mapVO.cellSize)*_model.mapVO.cellSize;
							
							_selectAsset.position=pt;
						}
						else
						{
							_selectAsset.position=IsoUtils.screenToSpace(_selectAsset.getScreenPoint());
						}
						
						dispatchEvent(new MapEditorEvent(MapEditorEvent.MAP_SELECT_ASSET_POS_CHANGE, _selectAsset.position));
					}
					_mapViewBase.depthSort();
					break;
			}
		}
		
		private function __onMouseOver(evt:MouseEvent):void
		{
			switch(_model.toolType)
			{
				case ToolType.TOOL_MOVE:
					Mouse.show();
					Mouse.cursor=MouseCursor.HAND;
					break;
				case ToolType.ROAD_BLOCK:
				case ToolType.CLEAR_ROAD_BLOCK:
					Mouse.show();
					Mouse.cursor=MouseCursor.BUTTON;
					break;
				case ToolType.MAP_ADD_ASSET:
					//					Mouse.hide();
					_mouseIsTool=false;
					if(_mouseAsset) _mouseAsset.visible=true;
					break;
				case ToolType.TOOL_ZOOM_IN:
				case ToolType.TOOL_ZOOM_OUT:
					//					Mouse.hide();
					_mouseIsTool=true;
					if(_mouseAsset) _mouseAsset.visible=true;
					break;
				default:
					_mouseIsTool=false;
					Mouse.show();
					Mouse.cursor=MouseCursor.AUTO;
					break;
			}
		}
		
		private function __onMouseOut(evt:MouseEvent):void
		{
			Mouse.cursor=MouseCursor.AUTO;
			switch(_model.toolType)
			{
				case ToolType.MAP_ADD_ASSET:
				case ToolType.TOOL_ZOOM_IN:
				case ToolType.TOOL_ZOOM_OUT:
					removeMouseAsset();
					break;
			}
		}
		
		/**
		 *将置于地图的资源物件跟随鼠标显示 
		 */
		private function addMouseAsset():void
		{
			clearMouseAsset();
			if(!_mouseIsTool)
			{//如果鼠标跟随物件不为工具图标，则是地图物件
				_mouseAsset=new MapAssetBase(_model.mouseAssetLink, true, _model.assetPoint);
				if(_model.assetIsMirror)
				{
					_mouseAsset.scaleX=-1;
				}
				
				if(_model.assetIsFloor)
				{
					_mapViewBase.addToFloor(_mouseAsset);
				}else
				{
					_mapViewBase.addToAsset(_mouseAsset);
				}
			}
			else
			{
				_mouseAsset=new MapAssetBase();
				switch(_model.toolType)
				{
					case ToolType.TOOL_ZOOM_IN:
						_mouseAssetDis=new Bitmap(new toolZoomInAsset() as BitmapData);
						break;
					case ToolType.TOOL_ZOOM_OUT:
						_mouseAssetDis=new Bitmap(new toolZoomOutAsset() as BitmapData);
						break;
				}
				_mouseAsset.asset=_mouseAssetDis;
				parent.addChild(_mouseAsset);
			}
			
			_mouseAsset.mouseEnabled=false;
			_mouseAsset.visible=false;
		}
		
		/**
		 *清除跟随鼠标的物件资源 
		 */		
		private function clearMouseAsset():void
		{
			if(_mouseAsset)
			{
				if(!_mouseIsTool)
				{
					if(_model.mapVO.assetArray.indexOf(_mouseAsset)!=-1) _model.mapVO.assetArray.splice(_model.mapVO.assetArray.indexOf(_mouseAsset), 1);
					_mapViewBase.removeAsset(_mouseAsset);
				}
				
				if(_mouseAssetDis && _mouseAssetDis.parent) _mouseAssetDis.parent.removeChild(_mouseAssetDis);
				if(_mouseAssetDis is Bitmap)
				{
					var bm:Bitmap=_mouseAssetDis as Bitmap;
					if(bm) bm.bitmapData.dispose();
					bm.bitmapData=null;
					bm=null;
				}
			}
			_mouseAssetDis=null;
			_mouseAsset=null;
		}
		
		/**
		 *移除跟随鼠标的物件资源（不清空其对象）
		 */		
		private function removeMouseAsset():void
		{
			Mouse.show();
			Mouse.cursor=MouseCursor.AUTO;
			if(_mouseAsset) _mouseAsset.visible=false;
		}
		
		/**
		 *移动地图(自已) 
		 */
		private function moveMapScene():void
		{
			if(_model.toolType==ToolType.TOOL_MOVE) startDrag();
		}
		
		/**
		 *创建测试对象 
		 */		
		private function testMap():void
		{
//			_mapGridLayer.alpha=0.0;
//			_roadGridLayer.visible=false;
			
			if(!_testBox)
			{
				_testBox = new MapAssetBase();
				var bm:Bitmap=new Bitmap(new renAsset());
				bm.x=-bm.width/2;
				bm.y=-bm.height;
				_testBox.asset=bm;
				_testBox.position=new IsoPoint(1 * _model.mapVO.cellSize, 0, 1 * _model.mapVO.cellSize);
				_mapViewBase.addToAsset(_testBox);
			}
			
			if(!_mapAStar) _mapAStar=new AStar(_model.mapVO.roadArray);
		}
		
		private function boxMove(targetPos:Point):void
		{
			if(!_testBox) return;
			
			_targetPos=IsoUtils.screenToSpace(targetPos);
			
			var targetGridPoint:IsoPoint=getGridPoint(_targetPos);
			var boxGridPoint:IsoPoint = getGridPoint(_testBox.position);
			
			_mapPathArray = _mapAStar.find(boxGridPoint.x, boxGridPoint.z, targetGridPoint.x, targetGridPoint.z);
			if (_mapPathArray != null && _mapPathArray.length > 0)
			{
				_mapPathArray.shift();
				_mapMoveNode=_mapPathArray[0] as Array;
				_actionTimer.start();
			}
		}
		
		private function getGridPoint(pt:IsoPoint):IsoPoint
		{
			return new IsoPoint(Math.floor(pt.x/_model.mapVO.cellSize), 0, Math.floor(pt.z/_model.mapVO.cellSize));
		}
		
		/**
		 *移动测试地图用盒子 
		 */		
		private function onTestBoxMove(evt:TimerEvent):void
		{
			if (_mapPathArray == null)
			{
				_actionTimer.stop();
				return;
			}
			
			if (_mapMoveNode)
			{
				_testBox.position=new IsoPoint(_mapMoveNode[0] * _model.mapVO.cellSize+(_model.mapVO.cellSize/4)*3, 0, _mapMoveNode[1] * _model.mapVO.cellSize+(_model.mapVO.cellSize/4)*3);
				_mapViewBase.depthSort();
				_mapMoveNode = _mapPathArray.shift() as Array;
			}
			else
			{
				_actionTimer.stop();
			}
			return;
			
			if(_mapPathArray.length>0)
			{
				var dx:Number = _mapMoveNode[0] * _model.mapVO.cellSize + _model.mapVO.cellSize - _testBox.x;
				var dy:Number = _mapMoveNode[1] * _model.mapVO.cellSize + _model.mapVO.cellSize - _testBox.y;
				var angle:Number = Math.atan2(dy, dx);
				var vx:Number = Math.cos(angle) * _mapSpeed;
				var vy:Number = Math.sin(angle) * _mapSpeed;
				
				var newPoint:Point=new Point(_testBox.x+vx, _testBox.y+vy);
				var distance1:Number=Point.distance(new Point(_testBox.x, _testBox.y), new Point(_mapMoveNode[0] * _model.mapVO.cellSize+_model.mapVO.cellSize, _mapMoveNode[1] * _model.mapVO.cellSize+_model.mapVO.cellSize));
				var distance2:Number=Point.distance(newPoint, new Point(_testBox.x, _testBox.y));
				
				var dis:Number=distance1-distance2;
				if(dis>0 && dis<=_mapSpeed)
				{
					_mapMoveNode = _mapPathArray.shift() as Array;
				}
				else if(dis<0 && dis>=-_mapSpeed)
				{
					_mapMoveNode = _mapPathArray.shift() as Array;
				}
			}
			
			if(_mapPathArray.length>0)
			{
				_testBox.moveTo(_testBox.x+vx, _testBox.y+vy, 0);
			}
			else
			{
				var pt:IsoPoint=abc(_targetPos);
				if(pt.x<=0 && pt.y<=0)
				{
					_testBox.moveTo(_targetPos.x, _targetPos.y, 0);
				}
				else
				{
					_testBox.moveTo(_testBox.x+pt.x, _testBox.y+pt.y, 0);
				}
			}
			//			setCenter();
		}
		
		private function abc(pt:IsoPoint):IsoPoint
		{
			var dx:Number = pt.x - _testBox.x;
			var dy:Number = pt.y - _testBox.y;
			var angle:Number = Math.atan2(dy, dx);
			var vx:Number = Math.cos(angle) * _mapSpeed;
			var vy:Number = Math.sin(angle) * _mapSpeed;
			
			var pt:IsoPoint  = new IsoPoint(vx, vy);
			
			return pt;
		}
		
		/**
		 * 地图场景放置物件
		 * @param dis 物件对象
		 * @param pt 坐标
		 */		
		private function addMapAsset(pt:Point):void
		{
			var mapAssetBase:MapAssetBase=new MapAssetBase(_model.assetLink, true, _model.assetPoint);
			mapAssetBase.mapAssetVO.link=_model.assetLink;
			mapAssetBase.mapAssetVO.isFloor=_model.assetIsFloor;
			mapAssetBase.mapAssetVO.isBottom=_model.assetIsBottom;
			mapAssetBase.mapAssetVO.isGrid=_model.assetIsGrid;
			mapAssetBase.mapAssetVO.isMirror=_model.assetIsMirror;
			mapAssetBase.mapAssetVO.assetName=_model.assetName;
			if(_model.assetIsMirror)
			{
				mapAssetBase.scaleX=-1;
			}
			
			if(_model.assetIsGrid)
			{//如果当前物件需要贴近格子存放
				var pd:IsoPoint=IsoUtils.screenToSpace(pt);
				//				pd.x=pd.x+isoAsset.height;
				//				pd.z=pd.z+isoAsset.height;
				pd.x=Math.floor(pd.x/_model.mapVO.cellSize)*_model.mapVO.cellSize;
				pd.y=Math.floor(pd.y/_model.mapVO.cellSize)*_model.mapVO.cellSize;
				pd.z=Math.floor(pd.z/_model.mapVO.cellSize)*_model.mapVO.cellSize;
				mapAssetBase.position = pd;
				
//				trace(pd.x, pd.y, pd.z);
//				trace(pd.x/30, pd.y/30, pd.z/30);
//				var tp:Point=IsoUtils.spaceToScreen(pd);
//				trace(tp.x/30+","+tp.y/30);
//				trace("-----------------");
//				return;
				//取得当前格子对象
				var mapGridVO:MapGridVO=_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)] as MapGridVO;
				
				
				
				if(_model.assetIsFloor)
				{//如果当前是置于地表层，并且需要贴近格子存放
					if(mapGridVO.mapAssetFloorBase)
					{
						if(_model.mapVO.assetArray.indexOf(mapGridVO.mapAssetFloorBase)!=-1) _model.mapVO.assetArray.splice(_model.mapVO.assetArray.indexOf(mapGridVO.mapAssetFloorBase), 1);
						_mapViewBase.removeAsset(mapGridVO.mapAssetFloorBase);//清除要增加到格子里原有的物件对象(如果是要贴近的对象，不管是什么类型的物件，每个格子只允许放一个)
						mapGridVO.mapAssetFloorBase.dispose();
					}
					mapGridVO.mapAssetFloorBase=null;
					mapGridVO.mapAssetFloorBase=mapAssetBase;//重新设置地图格子对象信息
				}
				else
				{
					//从地图物件列表删除该资源对象(格子原资源对象已被从地图中移除)
					if(mapGridVO.mapAssetBase)
					{
						if(_model.mapVO.assetArray.indexOf(mapGridVO.mapAssetBase)!=-1)  _model.mapVO.assetArray.splice(_model.mapVO.assetArray.indexOf(mapGridVO.mapAssetBase), 1);
						_mapViewBase.removeAsset(mapGridVO.mapAssetBase);//清除要增加到格子里原有的物件对象(如果是要贴近的对象，不管是什么类型的物件，每个格子只允许放一个)
						mapGridVO.mapAssetBase.dispose();
					}
					mapGridVO.mapAssetBase=null;
					mapGridVO.mapAssetBase=mapAssetBase;//重新设置地图格子对象信息
				}
			}
			else
			{
				//				pt.y=pt.y+isoAsset.height;
				//				pt.x=pt.x+isoAsset.width/2;
				mapAssetBase.position=IsoUtils.screenToSpace(pt);
			}
			
			if(_model.assetIsFloor)
			{
				_mapViewBase.addToFloor(mapAssetBase);
			}
			else
			{
				_mapViewBase.addToAsset(mapAssetBase);
			}
			_model.mapVO.assetArray.push(mapAssetBase);//将当前地图物件对象增加到地图物件列表
		}
		
		private function assetSelect(evt:MouseEvent):void
		{
			var arrObject:Array = getObjectsUnderPoint(new Point(stage.mouseX, stage.mouseY));
			arrObject=arrObject.reverse();
			if(arrObject && arrObject.length>0)
			{
				if(_selectAsset)
				{
					_selectAsset.stopDrag();
				}
				
				for(var i:int=0;i<arrObject.length;i++)
				{
					_selectAsset=arrObject[i] as MapAssetBase;
					if(!_selectAsset) _selectAsset=(arrObject[i].parent) as MapAssetBase;
					if(_selectAsset) break;
				}
				
				if(_selectAsset)
				{
					if(_selectAssetAcBox.parent) _selectAssetAcBox.parent.removeChild(_selectAssetAcBox)
					_selectAssetAcBox.graphics.clear();
					_selectAssetAcBox.graphics.lineStyle(1, 0x00a8ff, 1);
					_selectAssetAcBox.graphics.moveTo(0, 0);
					_selectAssetAcBox.graphics.lineTo(_selectAsset.width, 0);
					_selectAssetAcBox.graphics.lineTo(_selectAsset.width, _selectAsset.height);
					_selectAssetAcBox.graphics.lineTo(0, _selectAsset.height);
					_selectAssetAcBox.graphics.lineTo(0, 0);
					_selectAssetAcBox.x=-_selectAssetAcBox.width/2;
					_selectAssetAcBox.y=-_selectAssetAcBox.height;
					_selectAsset.addChild(_selectAssetAcBox);
					
					_selectAsset.startDrag();
					dispatchEvent(new MapEditorEvent(MapEditorEvent.MAP_SELECT_ASSET, _selectAsset));
				}
			}
		}
		
		private function __onSelectAssetEnterFrame(evt:Event):void
		{
			if(_model.toolType==ToolType.TOOL_SELECT && _model.assetIsGrid)
			{
				var pt:IsoPoint=IsoUtils.screenToSpace(new Point(_mapGridLayer.mouseX, _mapGridLayer.mouseY));
				pt.x=Math.floor(pt.x/_model.mapVO.cellSize)*_model.mapVO.cellSize;
				pt.y=Math.floor(pt.y/_model.mapVO.cellSize)*_model.mapVO.cellSize;
				pt.z=Math.floor(pt.z/_model.mapVO.cellSize)*_model.mapVO.cellSize;
				
				_selectAsset.position=pt;
			}
		}
		
		/**
		 *更新地图网格 
		 */		
		public function updateMapGrid():void
		{
			_mapGridLayer.gridType=_model.mapVO.mapType;
			_mapGridLayer.rowSize=_model.mapVO.mapRow;
			_mapGridLayer.colsSize=_model.mapVO.mapCols;
			_mapGridLayer.cellSize=_model.mapVO.cellSize;
			_mapGridLayer.update();
		}
		
		/**
		 *更新地图路点 
		 */		
		public function updateMapRoad(roadList:Array):void
		{
			_roadGridLayer.update(roadList);
		}
		
		/**
		 *更新地图资源物件 
		 */
		public function updateMapAsset():void
		{
			_mapViewBase.clearAll();
			
			for each(var mab:MapAssetBase in _model.mapVO.assetArray)
			{
				mab.loadAsset();
				if(mab.mapAssetVO.isFloor)
				{
					_mapViewBase.addToFloor(mab);
				}
				else
				{
					_mapViewBase.addToAsset(mab);
				}
			}
		}
		
		public function get mapViewBase():IsoScene
		{
			return _mapViewBase;
		}
		
		public function set mapViewBase(value:IsoScene):void
		{
			_mapViewBase = value;
		}
		
		//		/**
		//		 * 地图居中
		//		 */		
		//		private function setCenter():void
		//		{
		//			var xf : Number = -(_testBox.x - stage.stageWidth / 2);
		//			var yf : Number = -(_testBox.y - stage.stageHeight / 2)+50;
		//			
		//			if(xf > 0)xf = 0;
		//			if(xf < stage.stageWidth - 1000)
		//				xf = stage.stageWidth - 1000;
		//			if(yf > 0)yf = 0;
		//			if(yf < stage.stageHeight - 600)
		//				yf = stage.stageHeight - 600;
		//			
		//			this.x = xf;
		//			this.y = yf;
		//		}
	}
}